package com.zhuojy.equity.mall.admin.service.salary;/**
 * @description: TODO
 * @author chen
 * @date 2024/4/8 19:41
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.constant.JwtConstants;
import com.zhuojy.equity.mall.admin.common.entity.bo.salary.PaySlipsBO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.PaySlipsPO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.PayStubItemsPO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.TSalaryPO;
import com.zhuojy.equity.mall.admin.common.entity.vo.salary.PaySlipsVO;
import com.zhuojy.equity.mall.admin.dal.mapper.salary.PaySlipsMapper;
import com.zhuojy.equity.mall.admin.manager.token.TokenManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: TODO
 * @author chen
 * @date 2024/4/8 19:41
 * @version 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PaySlipsService extends ServiceImpl<PaySlipsMapper, PaySlipsPO> {

    private final TSalaryService tSalaryService;

    private final PayStubItemsService payStubItemsService;

    private final TokenManager tokenManager;
    public List<TSalaryPO> getSalaryList() {
        List<TSalaryPO> list = tSalaryService.list();
        return list;
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    public void addPayrollItems(PaySlipsBO paySlipsBO, HttpServletRequest request) {
        String userNameFromToken = tokenManager.getUserNameFromToken(request.getHeader(JwtConstants.TOKEN_HEADER).substring(6));

        PaySlipsPO paySlipsPO = new PaySlipsPO();
        BeanUtils.copyProperties(paySlipsBO,paySlipsPO);
        paySlipsPO.setCreateTime(new Date());
        paySlipsPO.setCreateBy(userNameFromToken);
        save(paySlipsPO);

        PayStubItemsPO payStubItemsPO = new PayStubItemsPO();
        BeanUtils.copyProperties(paySlipsBO,payStubItemsPO);
        payStubItemsPO.setId(null);
        payStubItemsPO.setPaySlipsId(paySlipsPO.getId());

        payStubItemsService.save(payStubItemsPO);
    }

    public Map getPaySlipsList(PaySlipsBO paySlipsBO) {
        IPage<PaySlipsPO> page = new Page(paySlipsBO.getCurrentPage(),paySlipsBO.getPageSize());
        LambdaQueryWrapper<PaySlipsPO> lqw = Wrappers.lambdaQuery(PaySlipsPO.class);
        if(StringUtils.isNotEmpty(paySlipsBO.getPaySlipsName())){
            lqw.like(PaySlipsPO::getPaySlipsName,paySlipsBO.getPaySlipsName());
        }
        page = page(page,lqw);

        List<PaySlipsPO> list = page.getRecords();

        List<PaySlipsVO> paySlipsVOList = list.stream().map(paySlipsPO -> {
            PaySlipsVO paySlipsVO = new PaySlipsVO();
            BeanUtils.copyProperties(paySlipsPO, paySlipsVO);
            TSalaryPO one = tSalaryService.getOne(Wrappers.lambdaQuery(TSalaryPO.class).eq(TSalaryPO::getId, paySlipsPO.getSalaryId()));
            paySlipsVO.setSalaryName(one.getSchemeName());
            return paySlipsVO;
        }).collect(Collectors.toList());


        Map map = new HashMap();
        map.put("rows",paySlipsVOList);
        map.put("total",page.getTotal());
        return map;
    }

    public PaySlipsVO findPaySlip(Long id) {
        PaySlipsPO paySlipsPO = getOne(Wrappers.lambdaQuery(PaySlipsPO.class).eq(PaySlipsPO::getId, id));

        PayStubItemsPO payStubItemsPO = payStubItemsService.getOne(Wrappers.lambdaQuery(PayStubItemsPO.class).eq(PayStubItemsPO::getPaySlipsId, paySlipsPO.getId()));

        PaySlipsVO paySlipsVO = new PaySlipsVO();
        BeanUtils.copyProperties(paySlipsPO,paySlipsVO);
        paySlipsVO.setPaySlipsId(payStubItemsPO.getPaySlipsId());
        paySlipsVO.setWages(payStubItemsPO.getWages().split(","));
        paySlipsVO.setBonus(payStubItemsPO.getBonus().split(","));
        paySlipsVO.setDeductions(payStubItemsPO.getDeductions().split(","));
        paySlipsVO.setMiscellaneous(payStubItemsPO.getMiscellaneous().split(","));
        return paySlipsVO;
    }
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    public void updatePaySlips(PaySlipsBO paySlipsBO, HttpServletRequest request) {
        String userNameFromToken = tokenManager.getUserNameFromToken(request.getHeader(JwtConstants.TOKEN_HEADER).substring(6));

        PaySlipsPO paySlipsPO = new PaySlipsPO();
        BeanUtils.copyProperties(paySlipsBO,paySlipsPO);
        paySlipsPO.setUpdateTime(new Date());
        paySlipsPO.setUpdateBy(userNameFromToken);
        updateById(paySlipsPO);

        payStubItemsService.update(Wrappers.lambdaUpdate(PayStubItemsPO.class)
                .eq(PayStubItemsPO::getPaySlipsId,paySlipsBO.getId())
                .set(PayStubItemsPO::getWages,paySlipsBO.getWages())
                .set(PayStubItemsPO::getBonus,paySlipsBO.getBonus())
                .set(PayStubItemsPO::getDeductions,paySlipsBO.getDeductions())
                .set(PayStubItemsPO::getMiscellaneous,paySlipsBO.getMiscellaneous()));
    }
}
